package com.geovis.emergency.spd.controller.basic;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.basic.service.IBasicThirdPartyInterfaceService;
import com.geovis.emergency.spd.entity.basic.entity.BasicThirdPartyInterface;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicThirdPartyInterfaceQueryDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicThirdPartyInterfaceSaveDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicThirdPartyInterfaceUpdateDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * <p>
 * 基础模块-集成接口 前端控制器
 * </p>
 *
 * @author 王响
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/basic/basicThirdPartyInterface")
@RequiredArgsConstructor
@Api(value = "基础模块-集成接口接口", tags = "基础模块-集成接口接口")
@Slf4j
@Validated
public class BasicThirdPartyInterfaceController extends BaseController<IBasicThirdPartyInterfaceService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping("/save")
    public Result<String> save(@RequestBody @Validated BasicThirdPartyInterfaceSaveDTO saveDTO) {
        BasicThirdPartyInterface basicThirdPartyInterface = BeanUtil.copyProperties(saveDTO, BasicThirdPartyInterface.class);
        baseService.save(basicThirdPartyInterface);
        return Result.ok();
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated BasicThirdPartyInterfaceUpdateDTO updateDTO) {
        BasicThirdPartyInterface basicThirdPartyInterface = baseService.getById(updateDTO.getId());
        Assert.notNull(basicThirdPartyInterface,"id:{},库里没有查询要相关数据");
        BeanUtil.copyProperties(updateDTO,basicThirdPartyInterface);
        baseService.updateById(basicThirdPartyInterface);
        return Result.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result<String> removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = SwaggerConstants.LIST_PAGE_VALUE, notes = SwaggerConstants.LIST_PAGE_VALUE)
    @PostMapping("/listPage")
    public Result<PageResult<BasicThirdPartyInterface>> listPage(@RequestBody @Validated PageParam<BasicThirdPartyInterfaceQueryDTO> pageParam) {
        return Result.ok(baseService.listPage(pageParam));
    }

}
